﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using MathAssistLibrary;
using System.Diagnostics;

namespace SuperCalculator
{
    public partial class frmSuperCalculator : Form, IForm
    {
        public frmSuperCalculator() {
            InitializeComponent();
        }

        string previous = "";
        string text = "";
        Expression express = new Expression();
        // bool isEnter = false;

        string IForm.Text {
            get { return "计算器"; }
        }
        frmSuperCalculator frm;
        Form IForm.GetForm {
            get {
                if (frm == null || frm.IsDisposed) {
                    frm = new frmSuperCalculator();
                }
                return frm;
            }
        }

        private void rtfInput_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyData == Keys.Enter)
                e.Handled = true;
        }

        private void tsCalculate_Click(object sender, EventArgs e) {
            Calculator();
        }
        private void rtfInput_KeyUp(object sender, KeyEventArgs e) {
            tsslError.Text = "";
            //if ( 13 == e.KeyChar )
            if (e.KeyCode == Keys.Enter) {
                //isEnter = true;
                Calculator();
            }
        }
        private void rtfInput_TextChanged(object sender, EventArgs e) {
            //如果还是以前的内容
            if (rtfCommond.Text.StartsWith(previous)) {
                //if ( isEnter )
                //{
                //    text = text.Replace( "\n", "" );
                //    rtfCommond.Text = previous + text;
                //    isEnter = false;
                //    return;
                //}
                text = rtfCommond.Text.Substring(previous.Length);
            } else {
                rtfCommond.Text = previous + text;
                //XBC.ControlComputer.PalyBeep();
                SetSelection();
            }
            //
            //
            //SetSelection();
        }

        private void Calculator() {
            try {
                Stopwatch sw = Stopwatch.StartNew();
                express.Format = text;
                previous += text + "\nanswer:" + express.Calculator().ToString() + "\n>>";
                sw.Stop();
                tsslTime.Text = string.Format("计算过程用时:{0}秒", sw.Elapsed.TotalSeconds.ToString("F3"));
                text = "";
                rtfCommond.Text = previous;
                SetSelection();
                //rtfCommond.SelectionStart = 0;
                TreeNode head = new TreeNode(express.Head.ToString()) { Tag = express.Head };
                AddNode(head, express.Head.Nexts);
                treeViewSyntax.Nodes.Clear();
                treeViewSyntax.Nodes.Add(head);
            } catch (ExpressionException value) {
                rtfCommond.Text = previous + text;
                tsslError.Text = string.Format("表达式出现错误:{0}，出错位置{1}，出错长度{2}", value.Message, value.Index, value.Length);
                rtfCommond.SelectionStart = previous.Length + value.Index;
                rtfCommond.SelectionLength = value.Length;
            }
        }

        void AddNode(TreeNode tree, List<Node> nodes) {
            foreach (Node i in nodes) { //1 I l 
                TreeNode n = new TreeNode(i.ToString());
                n.Tag = i;
                AddNode(n, i.Nexts);
                tree.Nodes.Add(n);
            }
        }

        private void SetSelection() {
            rtfCommond.SelectionStart = rtfCommond.Text.Length;
        }

        private void frmSuperCalculator_Load(object sender, EventArgs e) {
            List<FunctionNode> funs = new List<FunctionNode>();
            List<ConstantNode> cons = new List<ConstantNode>();
            List<OperateNode> ops = new List<OperateNode>();
            ReflectWord.Find(cons, funs, ops);
            TreeNode funNode = new TreeNode("函数名");
            foreach (FunctionNode f in funs) {
                TreeNode n = new TreeNode(f.Format) { Tag = f };
                funNode.Nodes.Add(n);
            }
            TreeNode conNode = new TreeNode("常量");
            foreach (ConstantNode c in cons) {
                TreeNode n = new TreeNode(c.Format) { Tag = c };
                conNode.Nodes.Add(n);
            }
            TreeNode opNode = new TreeNode("操作符");
            foreach (OperateNode o in ops) {
                TreeNode n = new TreeNode(o.Format) { Tag = o };
                opNode.Nodes.Add(n);
            }
            treeViewKeyword.Nodes.Add(funNode);
            treeViewKeyword.Nodes.Add(conNode);
            treeViewKeyword.Nodes.Add(opNode);

            rtfCommond.Text = ">>";
            previous = rtfCommond.Text;
            text = "";
        }

        private void treeViewKeyword_AfterSelect(object sender, TreeViewEventArgs e) {
            if (e.Node.Tag != null) {
                rtfDescription.Text = (e.Node.Tag as ExpendNode).Description;
            }
        }

        private void treeViewSyntax_AfterSelect(object sender, TreeViewEventArgs e) {
            if (e.Node.Tag != null) {
                propertyGridTreeNode.SelectedObject = e.Node.Tag;
            }
        }

        private void 粘贴PToolStripButton_Click(object sender, EventArgs e) {
            rtfCommond.Paste();
        }

        private void 剪切UToolStripButton_Click(object sender, EventArgs e) {
            rtfCommond.Cut();
        }

        private void 复制CToolStripButton_Click(object sender, EventArgs e) {
            rtfCommond.Copy();
        }

        private void tsmiClear_Click(object sender, EventArgs e) {
            previous = ">>";
            text = "";
            rtfCommond.Text = "";
        }

        private void frmSuperCalculator_FormClosing(object sender, FormClosingEventArgs e) {

        }

        private void 帮助LToolStripButton_Click(object sender, EventArgs e) {
            AboutBox1 frm = new AboutBox1();
            frm.ShowDialog();
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void tsmiCalculate_Click(object sender, EventArgs e) {
            Calculator();
        }

        private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e) {
            rtfCommond.Cut();
        }

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e) {
            rtfCommond.Copy();
        }

        private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e) {
            rtfCommond.Paste();
        }

        private void 关于AToolStripMenuItem_Click(object sender, EventArgs e) {
            AboutBox1 frm = new AboutBox1();
            frm.ShowDialog();
        }

    }
}
